﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Spring.Context;
using Spring.Context.Support;
using GONES.Tools.Common;

/// <summary>
/// Summary description for CPage
/// </summary>
public class CPage : Page
{
    protected IApplicationContext AppContext;
    private IUserInfo _userInfo = null;

    #region 分页变量，与UI绑定
    private int numPerPage;
    /// <summary>
    /// 每页显示的条数
    /// </summary>
    public int NumPerPage
    {
        get
        {
            int temp = Convert.ToInt32(Request.Form["numPerPage"]);
            return temp == 0 ? 20 : temp;
        }
        set { numPerPage = value; }
    }

    private int pageNumShown = 10;
    /// <summary>
    /// 页数导航的个数
    /// </summary>
    public int PageNumShown
    {
        get { return pageNumShown; }
        set { pageNumShown = value; }
    }

    private int pageNum;
    /// <summary>
    /// 当前显示的页数
    /// </summary>
    public int PageNum
    {
        get
        {
            int temp = Convert.ToInt32(Request.Form["pageNum"]);
            return temp == 0 ? 1 : temp;
        }
        set { pageNum = value; }
    }

    private long totalCount;
    /// <summary>
    /// 总条数
    /// </summary>
    public long TotalCount
    {
        get { return totalCount; }
        set { totalCount = value; }
    }

    private string keywords;
    /// <summary>
    /// where语句，不加where与空格
    /// </summary>
    public string Keywords
    {
        get
        {
            string temp = Request.Form["keywords"];
            return temp;
        }
        set { keywords = value; }
    }

    private string orderField;
    /// <summary>
    /// 排序关键字
    /// </summary>
    public string OrderField
    {
        get
        {
            string temp = Request.Form["orderField"];
            return temp;
        }
        set { orderField = value; }
    }
    #endregion
    public CPage()
    {
    }

    protected override void OnLoad(EventArgs e)
    {
        if (!UserInfo.IsOnLine)
        {
            if (Request.Path.EndsWith("Default.aspx"))//如果是首页，则直接转到登陆页
            {
                Response.Redirect("login.aspx");
                return;
            }

            Response.Write("<script type=\"text/javascript\">alert(\"操作超时或您没有登陆，系统将返回到登陆页！\");top.location.href=\"" +
                           ResolveClientUrl("login.aspx?from=" + HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.PathAndQuery)) + "\"</script>");
            return;
        }
        AppContext = ContextRegistry.GetContext();
        //TODO:只在测试时使用，正式发布时一定要去除
     //   HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize();
        base.OnLoad(e);   
    }

    public IUserInfo UserInfo
    {
        get
        {
            if (_userInfo != null)
                return _userInfo;

            _userInfo = new CWebUserInfo();
            return _userInfo;
        }
    }

    public void ScriptAlert(string msg)
    {
        this.ScriptAlert(msg, false);
    }

    public void ScriptAlert(string msg, bool back)
    {
        if (string.IsNullOrEmpty(msg))
            return;

        string t = msg.Replace("'", "\\'");
        t = t.Replace("\r", "\\r").Replace("\n", "\\n");
        t = string.Format("alert('{0}');", t);
        if (back)
            t += "history.back();";

        this.ClientScript.RegisterClientScriptBlock(this.GetType(), t, t, true);
    }

    public void ScriptAlert(string msg, string redirect)
    {
        if (string.IsNullOrEmpty(msg))
            return;

        string t = msg.Replace("'", "\\'");
        t = t.Replace("\r", "\\r").Replace("\n", "\\n");
        t = string.Format("alert('{0}');", t);
        if (!string.IsNullOrEmpty(redirect))
            t += string.Format("location.href='{0}';", redirect);

        this.ClientScript.RegisterClientScriptBlock(this.GetType(), t, t, true);
    }

    public void ScriptTip(string msg)
    {
        string t = "top.showTips('" + msg + "');";
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), t, t, true);
    }

    public void ScriptTip(string msg,bool back)
    {
        string t = "top.showTips('" + msg + "');";
        if (back)
            t += "history.back();";
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), t, t, true);
    }

    public void ScriptTip(string msg, string redirect)
    {
        string t = "top.showTips('" + msg + "');";
        if (!string.IsNullOrEmpty(redirect))
            t += string.Format("location.href='{0}';", redirect);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), t, t, true);
    }

    public void ScriptRun(string script)
    {
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), script, script.EndsWith(";") ? script : script + ";",
                                                    true);

    }
}